package com.hzhh123.test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hd.controller.activiti.util.cmd.ShareniuCountersignAddCmd;
import com.hd.controller.activiti.util.cmd.ShareniuCountersignMinCmd;
import com.hd.service.activiti.ActivitiService;
import com.hd.service.activiti.ElementDefService;
import com.hd.service.activiti.UserTaskService;
import com.hd.util.JsonUtil;
/**
 * 多实例
 * @author hzhh123
 * 2018年8月15日上午9:40:18
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class MultiInstanceTest4 {
	@Autowired
	private RepositoryService repositoryService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private HistoryService historyService;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private UserTaskService userTaskService;
	@Autowired
	private ActivitiService activitiService;
	@Autowired
	private ProcessEngine processEngine;
	@Autowired
	private ElementDefService elementDefService;
	/**
	 * 部署流程
	 * 部署id:20001
	 */
	@Test
	public void test1() {
		Deployment deploy = repositoryService.createDeployment().addClasspathResource("bpmn/sign.bpmn")
		.addClasspathResource("bpmn/sign.png").name("会签").deploy();
		System.out.println("部署id:"+deploy.getId());
		System.out.println("部署名称："+deploy.getName());
	}
		/**
		 * 测试串行
		 * @throws Exception
		 */
		@Test
	    public void testParallel() throws Exception {
			Map<String, Object> variables = new HashMap<String, Object>();
	        List<String> users = Arrays.asList("2", "3", "4");
	        variables.put("prevUserid", users);
	       // runtimeService.startProcessInstanceByKey("multiListener",variables);
	        runtimeService.startProcessInstanceByKey("sign");
	     /*   for (String userId : users) {
	            assertEquals(1, taskService.createTaskQuery().taskAssignee(userId).count());
	        }*/
	    }
		
		@Test
		public void test3() {
			String taskId="355004";
			  List<String> users = Arrays.asList("2", "3", "4");
			  Map<String, Object> variables = new HashMap<String, Object>();
		       variables.put("prevUserid", users);
			taskService.complete(taskId,variables);
		}
		@Test
		public void test4() {
			String processInstanceId="290001";
			Map<String,Object>map=findTodoTaskAssigneeOrCandidateUserIds(processInstanceId);
			System.out.println(JsonUtil.toJson(map));
		}
		
		public Map<String,Object> findTodoTaskAssigneeOrCandidateUserIds(String processInstanceId) {
			Map<String,Object>map=new HashMap<String,Object>();
			Set<String>userIds=new HashSet<String>();
			Set<String>taskNames=new HashSet<String>();
			List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
			if(tasks.size()>0) {
				for(Task task:tasks) {
					taskNames.add(task.getName());
					String assignee = task.getAssignee();
					if(assignee!=null) {
						userIds.add(assignee);
					}else {
						List<IdentityLink> identityLinks = taskService.getIdentityLinksForTask(task.getId());
						if(identityLinks.size()>0) {
							for(IdentityLink identityLink:identityLinks) {
								String type=identityLink.getType();
								if("participant".equals(type)) {
									//候选人
									userIds.add(identityLink.getUserId());
								}
								if("candidate".equals(type)) {
									//候选组
									List<org.activiti.engine.identity.User> users = identityService.createUserQuery().memberOfGroup(identityLink.getGroupId()).list();
									if(users.size()>0) {
										for(org.activiti.engine.identity.User user:users) {
											userIds.add(user.getId());
										}
									}
								}
							}
						}
					}
					
					}
			}
			map.put("userIds", userIds);
			map.put("taskName", taskNames.toString().replace("[", "").replace("]", ""));
			return map;
		}

		
		@Test
		public void test5() {
			List<String>deptids=Arrays.asList("225001","220001","215001","210001","200001");
			for(String deptid:deptids) {
			repositoryService.deleteDeployment(deptid, true);
			}
		}
		
		/**
		 * 测试加签
		 */
		@Test
		public void test6() {
			processEngine.getManagementService().executeCommand(new ShareniuCountersignAddCmd("362501","4"));
		}
		/**
		 * 测试减签
		 */
		@Test
		public void test7() {
			processEngine.getManagementService().executeCommand(new ShareniuCountersignMinCmd("362502"));
		}
		
		@Test
		public void test8() {
			elementDefService.init("sign:1:410004");
		}
}
